import {
  Login
} from '../../model/login'
Component({
  data: {
    register: false,
    Height: null,
    Width: null
  },
  properties: {
    register: {
      type: Boolean,
      value: false
    },
    Height: {
      type: Number,
      value: 0
    },
    Width: {
      type: Number,
      value: 0
    }
  },
  observers: {
    register(n, l) {
      // console.log(n);
    }
  },

  attached() {
    //进入时执行
  },
  methods: {
    getUserProfile(e) {
      login()
      wx.getUserProfile({
        lang: 'zh_CN',
        desc: '获取用户个人信息',
        success: res => {
          console.log(res);
          let sessionKey = wx.getStorageSync('session_key')
          let pardata = {
            sessionKey: sessionKey,
            signature: res.signature,
            iv: res.iv,
            encryptedData: res.encryptedData
          }
          wx.showLoading({
            title: '登录中..',
          })
          Login.postLogin(pardata).then(res => {
            console.log(res);
            wx.hideLoading()
            wx.setStorageSync('userInfo', res.data.data);
            // 关闭授权弹窗
            let pageArr = getCurrentPages();
            let thisPage = pageArr[pageArr.length - 1];
            thisPage.setData({
              register: false,
            });
            thisPage.onShow()
          })
        }
      })


      function login() {
        wx.login({
          success: res => {
            let code = res.code
            Login.getWxSessionKey({
              code
            }).then(res => {
              wx.setStorageSync('session_key', res.data.res.session_key)
              wx.setStorageSync('openid', res.data.res.openid)
            })
          }
        })
      }
    },
    closeModal() {
      this.setData({
        register: false
      })
    }
  },
});